<?php

namespace services\common;

use Yii;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use linslin\yii2\curl\Curl;
use common\components\Service;

/**
 * 志美大华电子秤
 *
 * 用法：
 *    Yii::$app->services->zhiMeiElectronicScaleService->analysis('220211300600003804');
 *
 * Class ZhiMeiElectronicScaleService
 * @package services\common
 * @author YiiFrame <21931118@qq.com>
 */
class ZhiMeiElectronicScaleService extends Service
{
    const URL = 'http://developer.dahuazm.com/api/developers';

    /**
     * 格式
     *
     * FWWWWWWC
     * FWWWWWWEEEEEC
     * FWWWWWWNNNNNC
     * FWWWWWWEEEEENNNNNC
     * FWWWWWWNNNNNEEEEEC
     * FWWWWWWEEEEENNNNNO
     * FWWWWWWNNNNNEEEEEO
     * FFWWWWWEEEEEC
     * FFWWWWWNNNNNC
     * DDDDDDDDDDDDC
     * FFWWWWWEEEEENNNNNC
     * FFWWWWWNNNNNEEEEEC
     * FFWWWWWEEEEENNNNNO
     * FFWWWWWNNNNNEEEEEO
     * FWWWWWEEEEEC+NNNNN
     *
     * F:店面
     * W:编码
     * E:金额
     * N:重量
     * C:校验
     * O:反校验
     * D：13码
     *
     * @var string
     */
    public $format = 'FFWWWWWEEEEENNNNNC';

    public function init()
    {
        // $this->app_id = Yii::$app->debris->backendConfig('electronic_scale_zhimei_app_id');
        // $this->secret_key = Yii::$app->debris->backendConfig('electronic_scale_zhimei_secret_key');
        // $this->format = Yii::$app->debris->backendConfig('electronic_scale_zhimei_format');;

        parent::init();
    }

    /**
     * @param $bar_code
     * @param string $format
     * @return array|float[]|int[]
     * @throws NotFoundHttpException
     */
    public function analysis($bar_code, $format = '')
    {
        empty($format) && $this->format = $format;

        if (strlen($bar_code) != strlen($this->format)) {
            throw new NotFoundHttpException('录入条码格式与设定的格式不符');
        }

        // 筛选位置
        $data = [];
        $local = $this->getLocation();
        foreach ($local as $key => $item) {
            if (empty($item[0]) && empty($item[1])) {
                unset($local[$key]);
            } else {
                $data[$key] = substr($bar_code, $item[0], $item[1] - $item[0] + 1);
                switch ($key) {
                    case 'money' :
                        $decimals = substr($data[$key], 3, 2);
                        $decimals > 0 && $decimals = $decimals / 100;
                        $data[$key] = (int) substr($data[$key], 0, 3) + $decimals;
                        break;
                    case 'weight' :
                        $decimals = substr($data[$key], 2, 3);
                        $decimals > 0 && $decimals = $decimals / 1000;
                        $data[$key] = (int) substr($data[$key], 0, 2) + $decimals;
                        break;
                }
            }
        }

        return [
            'store' => $data['store'] ?? 0,
            'encoding' => $data['encoding'] ?? 0,
            'money' => $data['money'] ?? 0,
            'weight' => $data['weight'] ?? 0,
        ];
    }

    /**
     * 同步到电子秤
     *
     * @param int $device_code 电子秤code
     * @param array $goods_info 商品信息
     * 示例：
     * $goods_info = [
     *     [
     *         'name' => '', // 商品名称
     *         'plu' => '', // PLU编号 如果没有 就拿商品编号填
     *         'produce_code' => '', // 商品码 如果没有 就拿商品编号填
     *         'shop_number' => '', // 商铺号 生成价签用，任意数字，同一个商铺统一即可
     *         'unit_price' => 0, // 商品单价
     *         'member_price' => 0, // 会员单价
     *     ]
     * ];
     *
     */
    public function import(array $goods_info)
    {
        $curl = new Curl();
        $result = $curl->setPostParams([
            'app_id' => $this->app_id,
            'secret_key' => $this->secret_key,
            'shop_number' => $this->shop_number,
            'goods_info' => $goods_info,
        ])->post(self::URL . '/goods/import');

        $result = Json::decode($result);

        p($result);die();
    }

    /**
     * 获取位置
     *
     * @return array[]
     */
    protected function getLocation()
    {
        return [
            'store' => [strpos($this->format, 'F'), strrpos($this->format, 'F')],
            'encoding' => [strpos($this->format, 'W'), strrpos($this->format, 'W')],
            'money' => [strpos($this->format, 'E'), strrpos($this->format, 'E')],
            'weight' => [strpos($this->format, 'N'), strrpos($this->format, 'N')],
        ];
    }
}